import os
import geopandas as gpd
import rasterio

from .utils import raster_to_shapefile, convert


def load(year, raster_file, meta_file, shape_file, out_file):
    """Loads data.

    Args:
        year (int): The year of interest.
        raster_file, meta_file (str): path to input files
        shape_file (str or geopandas.GeoDataFrame): input
            shapefile to outline boundaries
        out_file (str): output file, will auto load if existing

    Returns:
        df (geopandas.GeoDataFrame): The loaded dataset.
    """
    # check for file existence, load if it exists
    if os.path.isfile(out_file):
        print(f'Loading {out_file}')
        df = gpd.read_file(out_file)
    else:
        print(f'Creating {out_file}')
        if isinstance(shape_file, str):
            # read the input shape_file
            df = gpd.read_file(shape_file)
        elif isinstance(shape_file, gpd.GeoDataFrame):
            df = shape_file
        else:
            raise ValueError
        # read the raster
        raster = rasterio.open(raster_file)
        # read metadata for the raster
        meta = gpd.read_file(meta_file)
        # convert raster to shapefile
        df = raster_to_shapefile(
            df=df, raster=raster, col_name='cdl')
        # convert the values to strings of the categories
        df['cdl'] = convert(
            input_series=df['cdl'],
            input_type='val',
            metadata_value=meta['VALUE'],
            metadata_category=meta['CLASS_NAME'])
        # save the file
        df.to_file(out_file)
    return df
